﻿using System;
using System.IO;
using System.Text;
using System.Management;
using System.Diagnostics;
using System.ComponentModel;
using System.Collections.Generic;
using Microsoft.Web.Administration;
using Microsoft.Web.Management;
using System.Security;
using System.Windows.Forms;





namespace ConsoleApplication4
{
    class wmi
    {
        private const string MSSystem = "\\C$\\SecretAgent\\wwwRoot\\IncludeDirectories\\Mastersite\\System\\";

        static private void RunProcess(string ServerName, string process)
        {
            object[] theProcessToRun = { process };
            ManagementClass mClass = new ManagementClass(@"\\" + ServerName + @"\root\cimv2:Win32_Process");
            object result = mClass.InvokeMethod("Create", theProcessToRun);
        }

        //This will reset the IIS for the server. 
        static public Boolean IISReset(string ServerName)
        {
            try
            {
                RunProcess(ServerName, "iisreset.exe");
                return true;
            }
            catch
            {
                return false;
            }
        }

        //This takes server offline
        static public Boolean ServerOffline(string ServerName)
        {
            try
            {
                ServerName = "\\\\" + ServerName;
                File.SetAttributes(ServerName + MSSystem + "Status.aspx", FileAttributes.Normal);
                File.Delete(ServerName + MSSystem + "Status.aspx");
                File.Copy(ServerName + MSSystem + "StatusFail.aspx", ServerName + MSSystem + "Status.aspx");
                return true;
            }
            catch
            {
                return false;
            }
        }

        //This brings server online
        static public Boolean ServerOnline(string ServerName)
        {
            try
            {
                ServerName = "\\\\" + ServerName;
                File.SetAttributes(ServerName + MSSystem + "Status.aspx", FileAttributes.Normal);
                File.Delete(ServerName + MSSystem + "Status.aspx");
                File.Copy(ServerName + MSSystem + "StatusOriginal.aspx", ServerName + MSSystem + "Status.aspx");
                return true;

            }
            catch
            {
                return false;
            }
        }

        //This method export registry to file. 
        static public Boolean ExportReg(string RegKey, string SavePath, string ServerName)
        {
            string path = "\"" + SavePath + "\"";
            string key = "\"" + RegKey + "\"";
            string process = "regedit.exe /e " + path + " " + key + "";
            try
            {
                RunProcess(ServerName, process);
                return true;
            }
            catch
            {
                return false;
            }
        }

        //This method imports the registry file 
        static public Boolean ImportReg(string RegKey, string SavePath, string ServerName)
        {
            string path = "\"" + SavePath + "\"";
            string key = "\"" + RegKey + "\"";
            string process = "regedit.exe /s " + path + " " + key + "";
            try
            {
                RunProcess(ServerName, process);
                return true;
            }
            catch
            {
                return false;
            }
        }      

        //Retrieves CPU avaiable memory in MB for remote computer 
        public static void PerformanceMonitor(string FullComputerName, string Name, string PW)
        {
            try
            {
                ConnectionOptions connection = new ConnectionOptions();
                connection.Username = Name;
                connection.Password = PW;
                connection.Authority = "ntlmdomain:ENEIGHBORHOODS";

                ManagementScope scope = new ManagementScope(
                    "\\\\" + FullComputerName + "\\root\\CIMV2", connection);
                scope.Connect();

                ObjectQuery query = new ObjectQuery(
                    "SELECT * FROM Win32_PerfRawData_ASPNET_ASPNETApplications");
                ObjectQuery query1 = new ObjectQuery(
                    "SELECT * FROM Win32_PerfFormattedData_PerfOS_Memory");
                ObjectQuery query2 = new ObjectQuery(
                    "SELECT * Win32_PerfRawData_PerfDisk_PhysicalDisk");

                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher(scope, query);

                foreach (ManagementObject queryObj in searcher.Get())
                {
                //    Console.WriteLine("-----------------------------------");
                //    Console.WriteLine("AnonymousRequests: {0}", queryObj["AnonymousRequests"]);
                //    Console.WriteLine("-----------------------------------");
                //    Console.WriteLine("AnonymousRequestsPerSec: {0}", queryObj["AnonymousRequestsPerSec"]);
                //    Console.WriteLine("-----------------------------------");
                //    Console.WriteLine("RequestExecutionTime: {0}", queryObj["RequestExecutionTime"]);
                //    Console.WriteLine("-----------------------------------");
                //    //Console.WriteLine("RequestWaitTime: {0}", queryObj["RequestWaitTime"]);
                //    Console.WriteLine("-----------------------------------");
                    //Console.WriteLine("SessionActive: {0}", queryObj["SessionActive"]); 
                    MessageBox.Show("AnonymousRequests: ".ToString() + queryObj["AnonymousRequestsPerSec"].ToString() + "\n"
                        + "RequestExecutionTime: ".ToString() + queryObj["RequestExecutionTime"].ToString() +"\n" +
                      "RequestWaitTime: ".ToString() + queryObj["RequestWaitTime"].ToString(), "Servername" + FullComputerName, MessageBoxButtons.AbortRetryIgnore);
                                      
                }
               
                ManagementObjectSearcher searcher2 =
                    new ManagementObjectSearcher(scope, query1);
                foreach (ManagementObject queryObj in searcher2.Get())
                {
                    //Console.WriteLine("11-----------------------------------");
                    //Console.WriteLine("AvailableMBytes: {0}", queryObj["AvailableMBytes"]);
                    //Console.WriteLine("11-----------------------------------");
                    MessageBox.Show("AvailableMBytes: ".ToString() + queryObj["AvailableMBytes"].ToString(), "Servername" + FullComputerName, MessageBoxButtons.AbortRetryIgnore);
                }

                ManagementObjectSearcher searcher3 =
                                 new ManagementObjectSearcher(scope, query2);
                foreach (ManagementObject queryObj in searcher3.Get())
                {
                    //Console.WriteLine("11-----------------------------------");
                    //Console.WriteLine("AvgDiskQueueLength: {0}", queryObj["AvgDiskQueueLength"]);
                    //Console.WriteLine("11-----------------------------------");
                    MessageBox.Show("AvgDiskQueueLength: ".ToString() + queryObj["AvgDiskQueueLength"].ToString(), "Servername" + FullComputerName, MessageBoxButtons.AbortRetryIgnore);

                }

            }
            catch (ManagementException err)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + err.Message);
            }
        }
 
    }
}
   